
<script setup>
import { reactive,ref } from 'vue';
import { login } from '@/api/admin';
import {useAdminStore} from "@/stores/admin"

// 导入useRouter函数
import { useRouter } from 'vue-router';

//userStore
const userStore =useAdminStore()
//获取router对象
const router =useRouter()
const ruleForm =reactive({
  username:'',
  password:''


})
const rules =reactive({
  username:[
  { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 50, message: '长度为3-50', trigger: 'blur' },

  ],
  password:[
  { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 3, max: 15, message: 'Length should be 6 to 15', trigger: 'blur' },

  ]

})
const goToAdminLogin=()=> {
  window.location.href = 'http://localhost:5173/login';
}
// 获取到el-form元素的原生的dom对象
const ruleFormRef = ref()
const submitForm =  (formEl) => {
  if (!formEl) return
  // 手动触发el-form的validate方法来验证表单  ，validate时dom对象的函数
  
     formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      // 成功提交表单的操作，axios把用户名和密码请求后按api
      login(ruleForm).then(res => {
        console.log(res)
        // 把后端返回的token数据赋值给我们的全局变量
        userStore.saveToken(res.message)
        userStore.saveAdmin(res.data)
        console.log(res.data)
        //跳转
        router.push({
          path:"/admin/page",
        })
      })
      
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>
<template>
     <div id="app">
    <div class="login-wrapper">
      <!-- 装饰元素 -->
      <div class="decoration circle"></div>
      <div class="decoration square"></div>
      
      <!-- 顶部导航 -->
      <header class="login-header">
        <div class="header-content">
          <img src="data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><circle cx='50' cy='50' r='40' fill='%233498db'/><path d='M35,40 L50,60 L65,40 Z' fill='white'/></svg>" alt="东软颐养中心" class="brand-logo">
          <nav class="header-nav">
            <span class="nav-item">智慧养老管理系统</span>
            <span class="nav-item">统一身份认证平台</span>
          </nav>
        </div>
      </header>

      <!-- 登录主体 -->
      <main class="login-main">
        <!-- 左侧登录表单 -->
        <div class="login-card">
          <h1 class="card-title">管理员登录</h1>
          
          <el-form 
            ref="ruleFormRef" 
            :model="ruleForm" 
            :rules="rules" 
            label-width="auto"
            class="card-form"
          >
            <!-- 用户名 -->
            <el-form-item label="账号" prop="username">
              <el-input 
                v-model="ruleForm.username" 
                placeholder="请输入管理员账号"
              >
                <template #prefix>
                  <i class="fas fa-user"></i>
                </template>
                <template #prepend>admin@</template>
              </el-input>
            </el-form-item>

            <!-- 密码 -->
            <el-form-item label="密码" prop="password">
              <el-input 
                v-model="ruleForm.password" 
                type="password"
                placeholder="请输入6-15位密码"
                show-password
              >
                <template #prefix>
                  <i class="fas fa-lock"></i>
                </template>
                <template #append>
                  <el-tooltip content="密码规则：6-15位字符，包含字母和数字" placement="top">
                    <i class="fas fa-info-circle"></i>
                  </el-tooltip>
                </template>
              </el-input>
            </el-form-item>

            <!-- 登录按钮 -->
            <el-form-item class="login-btns">
              <el-button 
                type="primary" 
                :loading="loading" 
                @click="submitForm(ruleFormRef)"
                class="login-btn"
              >
                <i class="fas fa-sign-in-alt"></i> 立即登录
              </el-button>
              <el-button 
                type="default" 
                @click="handleReset"
                class="reset-btn"
              >
                <i class="fas fa-redo"></i> 重置密码
              </el-button>
            </el-form-item>

            <!-- 辅助功能 -->
            <div class="form-footer">
              <el-checkbox v-model="rememberMe">记住我</el-checkbox>
            </div>
          </el-form>

          <!-- 二维码登录 -->
          <div class="qr-login">
            <h3 class="qr-title">
              <i class="fas fa-qrcode"></i> 扫码快速登录
            </h3>
            <div class="qr-image">
              <img src="../assets/qrcode.png" alt="登录二维码" class="qr-code-img">
            </div>
            <p class="qr-desc">使用员工端APP扫码登录</p>
          </div>
        </div>
        
        <!-- 右侧系统介绍 -->
        <div class="system-info">
          <div class="info-content">
            <h2 class="welcome-title">智慧养老管理系统</h2>
            <p class="welcome-text">为老人提供全方位、智能化、个性化的养老服务，打造舒适、安全、便捷的养老环境</p>
            
            <div class="features">
              <div class="feature-item">
                <i class="fas fa-heartbeat"></i>
                <div>健康监测</div>
              </div>
              <div class="feature-item">
                <i class="fas fa-calendar-check"></i>
                <div>活动管理</div>
              </div>
              <div class="feature-item">
                <i class="fas fa-utensils"></i>
                <div>营养膳食</div>
              </div>
              <div class="feature-item">
                <i class="fas fa-shield-alt"></i>
                <div>安全保障</div>
              </div>
            </div>
          </div>
        </div>
      </main>

      <!-- 底部信息 -->
      <footer class="login-footer">
        <div class="footer-content">
          <nav class="footer-nav">
            <a href="/about" class="nav-link">关于我们</a>
            <a href="/contact" class="nav-link">联系我们</a>
            <a href="/privacy" class="nav-link">隐私政策</a>
            <a href="/terms" class="nav-link">服务条款</a>
            <a href="/help" class="nav-link">帮助中心</a>
          </nav>
          <p class="copyright">© 2024 东软颐养中心 版权所有 | 智慧养老解决方案提供商</p>
        </div>
      </footer>
    </div>
  </div>

</template>
<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

:root {
  --primary: #3498db;
  --primary-light: #5dade2;
  --primary-dark: #2874a6;
  --secondary: #1abc9c;
  --accent: #e74c3c;
  --light: #f8f9fa;
  --dark: #2c3e50;
  --gray: #95a5a6;
  --success: #2ecc71;
  --warning: #f39c12;
  --card-bg: rgba(255, 255, 255, 0.92);
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  --transition: all 0.3s ease;
}

body {
  background: linear-gradient(135deg, #f0f9ff 0%, #e6f7ff 100%), 
              url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" preserveAspectRatio="none"><path d="M0,0 L100,0 L100,100 Z" fill="%23d6eaf8" opacity="0.1"/></svg>');
  background-size: cover;
  background-position: center;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  position: relative;
  overflow: hidden;
}

/* 装饰元素 */
.decoration {
  position: absolute;
  z-index: 0;
}

.decoration.circle {
  width: 300px;
  height: 300px;
  border-radius: 50%;
  background: rgba(52, 152, 219, 0.1);
  top: -150px;
  right: -150px;
}

.decoration.square {
  width: 200px;
  height: 200px;
  background: rgba(26, 188, 156, 0.1);
  bottom: -100px;
  left: -100px;
  transform: rotate(45deg);
}

/* 主容器 */
.login-wrapper {
  width: 100%;
  max-width: 1000px;
  display: flex;
  flex-direction: column;
  z-index: 10;
  position: relative;
}

/* 顶部导航 */
.login-header {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 16px 16px 0 0;
  padding: 1rem 2rem;
  box-shadow: var(--shadow);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}
.qr-image {
  width: 200px; /* 容器宽度 */
  height: 200px; /* 容器高度 */
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center; /* 垂直居中 */
  background-color: #f5f5f5; /* 背景色（可选） */
  border-radius: 8px; /* 圆角（可选） */
}

.qr-code-img {
  max-width: 100%; /* 图片自适应容器 */
  max-height: 100%;
  border: none; /* 移除边框 */
}
.header-content {
  display: flex;
  align-items: center;
  gap: 20px;
  width: 100%;
}

.brand-logo {
  height: 50px;
  object-fit: contain;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.1));
}

.header-nav {
  display: flex;
  gap: 2rem;
  color: var(--dark);
  font-weight: 500;
  flex: 1;
  justify-content: center;
}

.nav-item {
  position: relative;
  padding: 8px 0;
  transition: var(--transition);
}

.nav-item::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 2px;
  background: var(--primary);
  transition: var(--transition);
}

.nav-item:hover {
  color: var(--primary);
}

.nav-item:hover::after {
  width: 100%;
}

/* 登录主体 */
.login-main {
  display: flex;
  background: var(--card-bg);
  border-radius: 0 0 16px 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
  min-height: 500px;
}

.login-card {
  flex: 1;
  padding: 3rem 2rem;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><rect width="100" height="100" fill="none" stroke="%23e3f2fd" stroke-width="0.5"/></svg>');
}

.card-title {
  color: var(--primary-dark);
  font-size: 2.2rem;
  margin-bottom: 2.5rem;
  text-align: center;
  position: relative;
  font-weight: 600;
}

.card-title::after {
  content: '';
  display: block;
  width: 80px;
  height: 4px;
  background: linear-gradient(to right, var(--primary), var(--secondary));
  margin: 12px auto 0;
  border-radius: 2px;
}

/* 表单样式 */
.card-form {
  max-width: 480px;
  margin: 0 auto;
  width: 100%;
}

.el-form-item {
  margin-bottom: 28px;
}

.el-form-item__label {
  font-weight: 500;
  color: var(--dark);
  padding-bottom: 8px;
  font-size: 16px;
}

.el-input__inner {
  border-radius: 12px !important;
  padding: 12px 20px !important;
  height: 48px !important;
  transition: var(--transition) !important;
  border: 1px solid #dcdfe6 !important;
}

.el-input__inner:focus {
  border-color: var(--primary) !important;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2) !important;
}

.el-input-group__prepend, .el-input-group__append {
  background: #f8f9fa !important;
  border: none !important;
  color: var(--primary-dark) !important;
  font-weight: 500;
}

/* 按钮样式 */
.login-btns {
  margin-top: 40px;
  display: flex;
  gap: 15px;
}

.login-btn, .reset-btn {
  flex: 1;
  height: 48px;
  border-radius: 12px !important;
  font-size: 16px !important;
  font-weight: 500 !important;
  letter-spacing: 0.5px;
  transition: var(--transition) !important;
}

.login-btn {
  background: linear-gradient(to right, var(--primary), var(--primary-light)) !important;
  border: none !important;
}

.login-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(52, 152, 219, 0.3) !important;
}

.reset-btn {
  background: #f8f9fa !important;
  color: var(--dark) !important;
  border: 1px solid #e9ecef !important;
}

.reset-btn:hover {
  background: #e9ecef !important;
  transform: translateY(-2px);
}

/* 表单底部 */
.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #eee;
}

.el-checkbox {
  color: var(--dark);
}

.forget-link {
  color: var(--primary);
  cursor: pointer;
  font-weight: 500;
  transition: var(--transition);
}

.forget-link:hover {
  color: var(--primary-dark);
  text-decoration: underline;
}

/* 二维码区域 */
.qr-login {
  margin-top: 40px;
  padding: 25px;
  background: linear-gradient(135deg, rgba(52, 152, 219, 0.05), rgba(26, 188, 156, 0.05));
  border-radius: 16px;
  text-align: center;
  border: 1px dashed var(--primary);
}

.qr-title {
  color: var(--primary-dark);
  margin-bottom: 15px;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.qr-image {
  width: 160px;
  height: 160px;
  margin: 0 auto 15px;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 50px;
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
}

.qr-desc {
  color: var(--dark);
  font-size: 14px;
  margin-top: 10px;
}

/* 系统介绍区域 */
.system-info {
  flex: 1;
  background: linear-gradient(135deg, var(--primary), var(--secondary));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  color: white;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.system-info::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 200"><circle cx="100" cy="100" r="85" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="1"/></svg>');
  animation: rotate 30s linear infinite;
}

@keyframes rotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.info-content {
  position: relative;
  z-index: 2;
  max-width: 400px;
}

.welcome-title {
  font-size: 2.2rem;
  font-weight: 600;
  margin-bottom: 1.5rem;
  text-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.welcome-text {
  font-size: 1.1rem;
  line-height: 1.6;
  margin-bottom: 2rem;
  opacity: 0.9;
}

.features {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  justify-content: center;
}

.feature-item {
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(5px);
  border-radius: 12px;
  padding: 15px;
  width: 150px;
  transition: var(--transition);
}

.feature-item:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: translateY(-5px);
}

.feature-item i {
  font-size: 2rem;
  margin-bottom: 10px;
}

/* 底部信息 */
.login-footer {
  padding: 2rem;
  text-align: center;
  color: var(--dark);
  margin-top: 30px;
  font-size: 0.9rem;
  background: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(5px);
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.footer-nav {
  display: flex;
  justify-content: center;
  gap: 2rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.nav-link {
  color: var(--dark);
  text-decoration: none;
  transition: var(--transition);
  position: relative;
  padding: 0 5px;
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: -3px;
  left: 0;
  width: 0;
  height: 2px;
  background: var(--primary);
  transition: var(--transition);
}

.nav-link:hover {
  color: var(--primary);
}

.nav-link:hover::after {
  width: 100%;
}

.copyright {
  color: var(--gray);
}

/* 响应式设计 */
@media (max-width: 992px) {
  .login-main {
    flex-direction: column;
  }
  
  .system-info {
    display: none;
  }
  
  .login-card {
    padding: 2rem 1.5rem;
  }
}

@media (max-width: 576px) {
  .login-header {
    flex-direction: column;
    gap: 15px;
    padding: 1rem;
  }
  
  .header-nav {
    gap: 1rem;
    flex-wrap: wrap;
  }
  
  .login-btns {
    flex-direction: column;
  }
  
  .footer-nav {
    gap: 1rem;
  }
}

/* 动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-card, .system-info {
  animation: fadeIn 0.8s ease-out;
}

.el-form-item {
  animation: fadeIn 0.5s ease-out;
}

.el-form-item:nth-child(1) { animation-delay: 0.1s; }
.el-form-item:nth-child(2) { animation-delay: 0.2s; }
.login-btns { animation-delay: 0.3s; }
.form-footer { animation-delay: 0.4s; }
.qr-login { animation-delay: 0.5s; }
</style>